// pages/list/list.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    heights:0,
    right_height:0,
    cate:[],
    aindex:0,
    pro:{},
    pro_url:'https://rt.gzziyu.com/',
    acindex:0,
    cat_id:"",
    title_index:0
  },
  tocomdetail(e) {
    var id = e.currentTarget.dataset.id; 
    wx.navigateTo({
      url: 'detail/detail?id='+id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var listname = app.globalData.listname;
    var that = this;
    // 计算高度
    var query = wx.createSelectorQuery();
    wx.getSystemInfo({
      success: function (res) {
        console.log(res.windowHeight)
        query.select('.search').boundingClientRect(function (rect) {
            that.setData({
              heights: res.windowHeight - rect.height
            })
          

        }).exec();
        query.select('.title').boundingClientRect(function (rect) {
          that.setData({
            right_height: rect.height
          })
          console.log(rect.height)

        }).exec();
      }
    })
    // 分类
    wx.request({
      url: app.globalData.url +'ny_flow.php?action=get_category',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success:function(res){
        console.log(res)
        that.setData({
          cate:res.data,
          cat_id: res.data[0].cat_id
        })
        var oneid = res.data[0].cat_id;
        console.log(listname)
        if (listname) {
          var arr = that.data.cate;
          for (var i = 0, len = arr.length; i < len; i++) {
            if (arr[i].cat_name == listname){
              oneid = arr[i].cat_id;
              that.setData({
                aindex:i
              })
            }
          }
        }
        
        // 通过分类获得商品
        wx.request({
          url: app.globalData.url +'category.php?id='+oneid,
          success:function(res){
            var arr = []
            for (var i in res.data) {
              arr.push(res.data[i]); //属性
            }
            console.log(arr,oneid)
            that.setData({
              pro:arr
            })
            
          }
        })
      }
    })
    
   
   
  },
  // 改变分类
  changecate:function(e){
    var that =this;
    console.log(e.target.dataset)
    var index = e.target.dataset.index;
    var id = e.target.dataset.id;
    if (index != this.data.aindex ){
      this.setData({
        aindex: index,
        cat_id:id
      })
      // 根据cat_id加载商品
      wx.request({
        url: app.globalData.url +'category.php?id=' + id,
        success: function (res) {

          var arr = []
          for (var i in res.data) {
            arr.push(res.data[i]); //属性
          }
          that.setData({
            pro: arr
          })
        }
      })
    }
  },
  // 改变筛选条件
  change_title:function(e){
    var that = this;
    that.setData({
      pro: []
    })
    var index = e.target.dataset.index;
    console.log(that.data.cat_id)
    console.log(index)
    this.setData({
      title_index:index
    })
    if(index == "0"){
      console.log('zonghe')
      wx.request({
        url: app.globalData.url +'ny_flow_goods.php?action=renqi',
        data: {
          id: that.data.cat_id
        },
        success: function (res) {
          console.log(res)
          wx.request({
            url: app.globalData.url + res.data,
            success: function (res) {
              console.log(res)
             
              that.setData({
                pro: res.data
              })
            }
          })
        }
      })
    }else if(index == "1"){
      console.log('xiaolaing')
      wx.request({
        url: app.globalData.url +'ny_flow_goods.php?action=xiaoliang',
        data: {
          id: that.data.cat_id
        },
        success: function (res) {
          console.log(res)
          wx.request({
            url: app.globalData.url + res.data,
            success: function (res) {
              console.log(res)
              
              that.setData({
                pro: res.data
              })
            }
          })
        }
      })
    }else if(index == "2"){
      console.log('jaige')
      // wx.request({
      //   url: 'http://rt.gzziyu.com/mobile/category.php?category=3&display=&brand=0&price_min=0&price_max=0&filter_attr=0&page=1&sort=shop_price&order=ASC&keywords=#goods_list',
      //   success:function(res){
      //     console.log(res)
      //     console.log(res)
            
      //         that.setData({
      //           pro: res.data
      //         })
      //   }
      // })
      wx.request({
        url: app.globalData.url+'ny_flow_goods.php?action=jiage',
        data: {
          id: that.data.cat_id
        },
        success: function (res) {
          console.log(res)
          wx.request({
            url: app.globalData.url + res.data,
            success: function (res) {
              console.log(res)
              var arr = []
              for (var i in res.data) {
                arr.push(res.data[i]); //属性
              }
              that.setData({
                pro: arr
              })
            }
          })
        }
      })
    }
  
  },
  search: function () {
    wx.navigateTo({
      url: '../search/search'
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // 分类
    // wx.request({
    //   url: app.globalData.url+'ny_flow.php?action=get_category',
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     that.setData({
    //       cate: res.data,
    //       cat_id: res.data[0].cat_id
    //     })
    //     var oneid = res.data[0].cat_id;
    //     if (app.globalData.listname) {
    //       var arr = that.data.cate;
    //       for (var i = 0, len = arr.length; i < len; i++) {
    //         if (arr[i].cat_name == app.globalData.listname) {
    //           oneid = arr[i].cat_id;
    //           that.setData({
    //             aindex: i
    //           })
    //         }
    //       }
    //     }

    //     // 通过分类获得商品
    //     wx.request({
    //       url: app.globalData.url+'category.php?id=' + oneid,
    //       success: function (res) {
    //         var arr = []
    //         for (var i in res.data) {
    //           arr.push(res.data[i]); //属性
    //         }
    //         that.setData({
    //           pro: arr
    //         })

    //       }
    //     })
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})